package com.sf.fy.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.entity.RecordModuleDuration;
import com.sf.fy.uav.mapper.RecordModuleDurationMapper;
import com.sf.fy.uav.service.IRecordModuleDurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LTQ
 * @since 2023-11-23
 */
@Service
public class RecordModuleDurationServiceImpl extends ServiceImpl<RecordModuleDurationMapper, RecordModuleDuration> implements IRecordModuleDurationService {

    @Autowired
    private RecordModuleDurationMapper recordModuleDurationMapper;

    @Override
    public Long countBySubjectId(String subjectId, String subject) {
        String userId = JwtUtil.getUserId();
        QueryWrapper<RecordModuleDuration> recordModuleDurationQueryWrapper = new QueryWrapper<>();
        recordModuleDurationQueryWrapper.eq("subject_id", subjectId);
        recordModuleDurationQueryWrapper.eq("subject", subject);
        recordModuleDurationQueryWrapper.eq("user_id", userId);
        return recordModuleDurationMapper.selectCount(recordModuleDurationQueryWrapper);
    }

    @Override
    public RecordModuleDuration selectLastBySubjectId(String subjectId, String subject) {
        String userId = JwtUtil.getUserId();
        QueryWrapper<RecordModuleDuration> recordModuleDurationQueryWrapper = new QueryWrapper<>();
        recordModuleDurationQueryWrapper.eq("subject_id", subjectId);
        recordModuleDurationQueryWrapper.eq("subject", subject);
        recordModuleDurationQueryWrapper.eq("user_id", userId);
        recordModuleDurationQueryWrapper.orderByDesc("create_time");
        recordModuleDurationQueryWrapper.last("limit 1");
        return recordModuleDurationMapper.selectOne(recordModuleDurationQueryWrapper);
    }

    @Override
    public List<RecordModuleDuration> selectRecordModuleDurationListBySubjectIds(List<String> ids) {
        return recordModuleDurationMapper.selectList(new QueryWrapper<RecordModuleDuration>().in("subject_id",ids));
    }
}
